package org.dromara.config.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.config.domain.bo.KyWarehousesBo;
import org.dromara.config.domain.vo.KyWarehousesVo;
import org.dromara.config.domain.KyWarehouses;
import org.dromara.config.mapper.KyWarehousesMapper;
import org.dromara.config.service.IKyWarehousesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * warehouseService业务层处理
 *
 * @author kuyu
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class KyWarehousesServiceImpl implements IKyWarehousesService {

    private final KyWarehousesMapper baseMapper;

    /**
     * 查询warehouse
     */
    @Override
    public KyWarehousesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询warehouse列表
     */
    @Override
    public TableDataInfo<KyWarehousesVo> queryPageList(KyWarehousesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KyWarehouses> lqw = buildQueryWrapper(bo);
        Page<KyWarehousesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询warehouse列表
     */
    @Override
    public List<KyWarehousesVo> queryList(KyWarehousesBo bo) {
        LambdaQueryWrapper<KyWarehouses> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KyWarehouses> buildQueryWrapper(KyWarehousesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KyWarehouses> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMid() != null, KyWarehouses::getMid, bo.getMid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), KyWarehouses::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), KyWarehouses::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getLogo()), KyWarehouses::getLogo, bo.getLogo());
        lqw.eq(bo.getApproveIn() != null, KyWarehouses::getApproveIn, bo.getApproveIn());
        lqw.eq(bo.getApproveOut() != null, KyWarehouses::getApproveOut, bo.getApproveOut());
        lqw.eq(bo.getProvinceId() != null, KyWarehouses::getProvinceId, bo.getProvinceId());
        lqw.eq(bo.getCityId() != null, KyWarehouses::getCityId, bo.getCityId());
        lqw.eq(bo.getDistrictId() != null, KyWarehouses::getDistrictId, bo.getDistrictId());
        lqw.eq(StringUtils.isNotBlank(bo.getAdress()), KyWarehouses::getAdress, bo.getAdress());
        lqw.eq(bo.getModifyConfirm() != null, KyWarehouses::getModifyConfirm, bo.getModifyConfirm());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), KyWarehouses::getMark, bo.getMark());
        lqw.eq(bo.getIsLocked() != null, KyWarehouses::getIsLocked, bo.getIsLocked());
        lqw.eq(StringUtils.isNotBlank(bo.getQrcode()), KyWarehouses::getQrcode, bo.getQrcode());
        lqw.eq(bo.getDeletedAt() != null, KyWarehouses::getDeletedAt, bo.getDeletedAt());
        lqw.eq(StringUtils.isNotBlank(bo.getLocalPath()), KyWarehouses::getLocalPath, bo.getLocalPath());
        return lqw;
    }

    /**
     * 新增warehouse
     */
    @Override
    public Boolean insertByBo(KyWarehousesBo bo) {
        KyWarehouses add = MapstructUtils.convert(bo, KyWarehouses.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改warehouse
     */
    @Override
    public Boolean updateByBo(KyWarehousesBo bo) {
        KyWarehouses update = MapstructUtils.convert(bo, KyWarehouses.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KyWarehouses entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除warehouse
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
